<?php
namespace Admin\Controller;


class SinglepageController extends CommonController{

	public function index() {
		$this -> page = M('singlepage') -> order('sort') -> select();
		$this -> display();
	}

	/**
	 * 添加单页方法
	 */
	public function add() {
		if (IS_POST) {
            $_POST['add_time']=time();
			if ( M('singlepage') -> add($_POST)) {
				$this -> success('添加成功！', U(MODULE_NAME . '/Singlepage/index'));
			} else {
				$this -> error('添加失败！');
			}
		} else {
			$this -> page = M('singlepage') -> select();
			$this -> display();
		}
	}
	
	// 修改单页
	public function edit() {
		if (IS_POST) {
			$id = I('id', 0, 'intval');
			// 获取要修改的栏目ID
			$name = $_POST['name'];;
			$sort = I('sort');
			$content = $_POST['content'];
			$where = array('id' => $id);
			$db = M('singlepage') -> where($where) ->save(array('name' => $name, 'sort' => $sort, 'content' => $content));
			if ($db) {
				$this -> success('修改成功！', U(MODULE_NAME . '/singlepage/index'));
			} else {
				$this -> error('修改失败！');
			}
		} else {
			$id = I('id', 0, 'intval');
			// 获取要修改的单页ID
			$single = M('singlepage') -> where(array('id' => $id)) -> find();

			$this -> single = $single;
			
			$this -> display();
		}
	}


    //异步排序
    public function change(){
        //更新当前栏目的排序
        $res = M('singlepage')->where(array('id'=>$_POST['id']))->save(array('sort'=>$_POST['sort']));
        if($res){
            $data =['status'=>1,'info'=>'更新成功！',];
        }else{
            $data = ['status'=>0,'info'=>'更新失败，请稍后再试！',];
        }
        $this->ajaxReturn($data);
    }




    // 删除
    public function delete() {
        $id = I('id', 0, 'intval');
        if (M('singlepage')->where(array('id' => $id))->delete()) {
            $data =['status'=>1,'info'=>'删除成功！',];
        }else{
            $data = ['status'=>0,'info'=>'删除失败，请稍后再试！',];
        }
        $this->ajaxReturn($data);
    }
}
